package leaf.science.pages
{
	import caurina.transitions.Tweener;
	
	import fl.motion.Animator;
	import fl.motion.MotionEvent;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.setTimeout;
	
	import leaf.science.Global;
	import leaf.science.nav.Page;
	import leaf.science.ui.leftmenu.LeftMenu;
	import leaf.science.ui.preloader.Preloader;

	public class PageSpecifications extends Page
	{
		private var _loader:Loader;
		private var _animator:Animator;
		private var _link:Sprite;
		
		[Embed (source="../library/jogWheelMotionIn.xml", mimeType="application/octet-stream")]
		private var MotionInDef:Class;
		private var motionInXML:XML = new XML(new MotionInDef());
		
		public var leftMenu:LeftMenu;
		public var preloader:Preloader;
		
		public function PageSpecifications()
		{
			super();
		}
		
		override public function goIn():void
		{
			init();
			_loader.alpha = 0;
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
			_loader.load(new URLRequest(Global.ROOT_PATH + 'swf/SpecTable.swf'));
		}
		
		private function onLoad(evt:Event):void
		{
			preloader.stop();
			removeChild(preloader);
			
			_link = Sprite(_loader.content).getChildByName('link_mc') as Sprite;
			_link.buttonMode = true;
			_link.addEventListener(MouseEvent.CLICK, onLink);
			
			_loader.content.x = - int(_loader.content.width/2) + 85;
			_loader.content.y = - int(_loader.content.height/2);
			//Tweener.addTween(_loader, {alpha:1, time:1, onComplete:function():void{leftMenu.open()}});
			_animator = new Animator(motionInXML, _loader);
			_animator.addEventListener(MotionEvent.MOTION_END, onTableIn);
			_animator.play();
		}
		
		private function onTableIn (evt:Event):void
		{
			leftMenu.open();
		}
		
		override public function goOut():void
		{
			leftMenu.close();
			setTimeout(tableOut, 300)
		}
		
		private function tableOut():void
		{
			_loader.filters = [new BlurFilter(0,0,2)];
			Tweener.addTween(_loader, {time:1, _blur_blurX:20, _blur_blurY:20, alpha:0, onComplete:onOutComplete});
		}
		
		private function onTableOut ():void
		{
			onOutComplete();
		}
		
		private function init():void
		{
			leftMenu = new LeftMenu();
			leftMenu.selectedName = this.toString();
			addChild(leftMenu);
			
			preloader = new Preloader();
			preloader.alpha = 0.5;
			addChild(preloader);
			
			_loader = new Loader();
			addChild(_loader);
		}
		
		private function onLink (evt:Event):void
		{
			navigateToURL(new URLRequest("http://www.leaf-photography.com/afi2/specs"), '_blank');
		}
		
		override public function toString():String
		{
			return 'specifications';
		}
		
		
	}
}